package scjp.threads.machineoperator;


import java.util.ArrayList;
import java.util.List;

public class Machine extends Thread {

    List<MachineInstructions> jobList = new ArrayList<MachineInstructions>();


    public void addJob(MachineInstructions job) {

        synchronized (jobList) {

            jobList.add(job);
            jobList.notify();
        }
    }

    @Override
    public void run() {

        while (true) {

            synchronized (jobList) {

                //wait until at least one job is available

                while (jobList.isEmpty()) {

                    try {
                        jobList.wait();
                    } catch (InterruptedException ignore) {
                    }

                }

                //If we get here, we know that jobs is not empty

                MachineInstructions instructions = jobList.remove(0);
                //Send machine steps to hardware.
            }
        }
    }
}
